package com.org.platform.support.conponents.delqueue;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @auther dh
 * @date 2022/4/10 6:35
 */
public class LinkQueueTest extends Thread {

	private LinkedBlockingQueue<String> triggerQueue;
	private volatile boolean toStop = false;


	public LinkQueueTest(int capacity) {
		this.triggerQueue = new LinkedBlockingQueue<String>(capacity);
	}

	public void pushQueue(String string) {
		triggerQueue.add(string);
	}

	public void toStop() {
		this.toStop = true;
	}

	@Override
	public void run() {
		while (!toStop) {
			String result = null;
			try {
				result = triggerQueue.poll(3L, TimeUnit.SECONDS);
				// result有可能为空
			} catch (Throwable throwable) {
				throwable.printStackTrace();
			}
			System.out.println(result);
		}
	}

	public static void main(String[] args) {
		LinkQueueTest linkQueueTest = new LinkQueueTest(10);
		linkQueueTest.start();
		for (int i = 0; i < 20; i++) {
			linkQueueTest.pushQueue("i=" + i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		linkQueueTest.toStop();
	}
}
